﻿Shader "URP/reflect"
{
    Properties
    {
        _MainTex("MainTex",2D)="white"{}
        _BaseColor("BaseColor",Color)=(1,1,1,1)
        //_ReflectColorRT("MainTex",2D)="white"{}
        [Normal]_NormalTex("NormalTex",2D)="bump"{}
        
        _ReflectNoise("ReflectNoise",Range(0,100))=20
        [Header(NORMAL1)]
        _Normal1Str("NormalScale1",Range(0,1))=1
        _scale1("Scale1",float)=1
        _flowSpeed1("FlowSpeed1",Range(-1,1))=0.5
        [Header(NORMAL2)]
        _Normal2Str("NormalScale2",Range(0,1))=1
        _scale2("Scale2",float)=-1
        _flowSpeed2("FlowSpeed2",Range(-1,1))=0.5
    }
    SubShader
    {
        Pass
        {
            Tags{
                "LightMode" = "SRPDefaultUnlit"
                "RenderType"="Overlay"
            }
            Blend SrcAlpha OneMinusSrcAlpha
            HLSLPROGRAM
             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            #pragma vertex vert
            #pragma fragment frag 

            struct a2v
             {
                 float4 positionOS:POSITION;
                 float4 normalOS:NORMAL;
                 float2 texcoord:TEXCOORD;
             };
             struct v2f
             {
                 float4 positionCS:SV_POSITION;
                 float2 texcoord:TEXCOORD;
                 float4 screenPos:TEXCOORD1  ;
                 //float3 posWS:TEXCOORD2 ;
                 float4 texcoordNor:TEXCOORD3;
             };

            CBUFFER_START(UnityPerMaterial)
            float4 _MainTex_ST;
            half4 _BaseColor;
            half _Normal1Str;
            half _Normal2Str;
            half _ReflectNoise;
            float _flowSpeed1;
            float _scale1;
            float _scale2;
            float _flowSpeed2;
            float4 _NormalTex_TexelSize;
            CBUFFER_END
            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);
            TEXTURE2D(_NormalTex);
            SAMPLER(sampler_NormalTex);

            TEXTURE2D(_ReflectColorRT);
            sampler LinearClampSampler;


            v2f vert(a2v i)
            {
                v2f o;
                o.positionCS = TransformObjectToHClip(i.positionOS.xyz);
                o.texcoord=i.texcoord;
                o.texcoordNor.xy=i.texcoord*_scale1+_Time.y*_flowSpeed1*0.05;
                o.texcoordNor.zw=i.texcoord*_scale2+_Time.y*_flowSpeed2*0.05;
                o.screenPos = ComputeScreenPos(o.positionCS);
                return o;
            }

            float3  NormalBlend(float3 A, float3 B)//从shadergraph里白嫖的算法
            {
                return normalize(float3(A.rg + B.rg, A.b * B.b));
            }

            float4 frag(v2f i) : SV_Target
            { 
                
                float4 texcolor=SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.texcoord);
                float4 normaltex1=SAMPLE_TEXTURE2D(_NormalTex,sampler_NormalTex,i.texcoordNor.xy);
                float3 TSnormal1=UnpackNormalScale(normaltex1,_Normal1Str);

                float4 normaltex2=SAMPLE_TEXTURE2D(_NormalTex,sampler_NormalTex,i.texcoordNor.zw);
                float3 TSnormal2=UnpackNormalScale(normaltex2,_Normal2Str);
                float3 normalMix=NormalBlend(TSnormal2,TSnormal1);

                float2 UVoffset= normalMix.xy * _NormalTex_TexelSize.xy * _ReflectNoise;

                half2 screenUV = i.screenPos.xy/i.screenPos.w;
                float4 SSPRResult = SAMPLE_TEXTURE2D(_ReflectColorRT,LinearClampSampler,screenUV+UVoffset);
				SSPRResult.xyz *= SSPRResult.w;
            
                //return SSPRResult * _BaseColor+texcolor;
               return SSPRResult;
            }

            ENDHLSL
        }
    }
}
